package com.changgou.seckill.mq.listener;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.SeckillStatus;
import com.changgou.seckill.service.SeckillOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 过期消息监听
 */
@Component
@RabbitListener(queues = "seckillQueue")
public class SeckillOrderDelayMessageListener {
    @Autowired
    private SeckillOrderService seckillOrderService;
    @Autowired
    protected RedisTemplate redisTemplate;

    @RabbitHandler
    public void GetDelayMessage(String message) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
            System.out.println("下单时间：" + simpleDateFormat.format(new Date()));
            SeckillStatus seckillStatus = JSON.parseObject(message, SeckillStatus.class);
            //如果此时Redis中没有用户排队信息，则表明该订单已经处理,如果有用户排队信息，则表示用户尚未完成支付
            Object userQueueStatus = redisTemplate.boundHashOps("UserQueueStatus").get(seckillStatus.getUserId());
            if (userQueueStatus != null) {
                //关闭微信支付 自己做
                seckillOrderService.deleteOrder(seckillStatus.getUserId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
